package com.atguigu.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.msmservice.service.MsmService;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther 赵乾坤
 * @Date 2020-11-30 15:26
 */
@Service
public class MsmServiceImpl implements MsmService {

    //根据手机号发送验证码
    @Override
    public boolean sendMsmPhone(String phone, Map<String, Object> param) {

        if(StringUtils.isEmpty(phone)) return false;

        //发送短信的初始化对象 default是默认的
        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAI4FyRtoRRhErEEi7GNGWE", "BrLCe0x84jkTta4fUb14n82oOHJlYg");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        //下面三个参数 是设定的参数
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "紫色的大象");
        request.putQueryParameter("TemplateCode", "SMS_205131945");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));


        //返回结果成功或失败
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"发送短信失败");
        }


    }



}
